﻿namespace Chain.Common
{
    using System;
    using System.Runtime.InteropServices;
    using System.Text;

    public static class INIFile
    {
        public static string path = (AppDomain.CurrentDomain.BaseDirectory + @"\config.ini");

        public static void ClearAllSection()
        {
            IniWriteValue(null, null, null);
        }

        public static void ClearSection(string Section)
        {
            IniWriteValue(Section, null, null);
        }

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defVal, byte[] retVal, int size, string filePath);
        public static string IniReadValue(string Section, string Key)
        {
            StringBuilder retVal = new StringBuilder(0x1388);
            GetPrivateProfileString(Section, Key, "", retVal, 0x1388, path);
            return retVal.ToString();
        }

        public static byte[] IniReadValues(string section, string key)
        {
            byte[] retVal = new byte[0x1388];
            GetPrivateProfileString(section, key, "", retVal, 0x1388, path);
            return retVal;
        }

        public static void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, path);
        }

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    }
}

